using System;
using System.Windows;

namespace Crusades.Helpers{
  public class Vector{
    private double x;

    public double X{
      get { return x; }
      set { x = value; }
    }

    private double y;

    public double Y{
      get { return y; }
      set { y = value; }
    }

    public Vector(double X, double Y){
      x = X;
      y = Y;
    }

    public override string ToString() {
      return string.Format("({0},{1})",x,y);
    }

    public override bool Equals(object obj) {
      if (ReferenceEquals(null, obj)) return false;
      if (ReferenceEquals(this, obj)) return true;
      if (obj.GetType() != typeof (Vector)) return false;
      return Equals((Vector) obj);
    }

    public static Vector operator+(Vector v1,Vector v2){
      return new Vector(v1.x + v2.x,v1.y + v2.y);
    }

    public static Vector operator-(Vector v1, Vector v2){
      return new Vector(v1.x-v2.x,v1.y-v2.y);
    }

    public static Vector operator/(Vector v1, double scalar){
      return new Vector(v1.x / scalar,v1.y/scalar);
    }

    public static Vector operator *(Vector v, double scalar){
      return new Vector(v.x*scalar,v.y*scalar);
    }

    public static Vector operator *(double scalar,Vector v){
      return new Vector(v.x*scalar,v.y*scalar);
    }

    public static bool operator ==(Vector v1, Vector v2){
      return v1.Equals(v2);
    }

    public static bool operator !=(Vector v1, Vector v2){
      return !(v1==v2);
    }

    public Point toPoint(){
      return new Point(X,Y);
    }

    public bool Equals(Vector obj, double delta){
      if (ReferenceEquals(null, obj)) return false;
      if (ReferenceEquals(this, obj)) return true;
      return Math.Abs(obj.x - x) <= delta && 
             Math.Abs( obj.y - y) <= delta;      
    }
    public bool Equals(Vector obj){
      if (ReferenceEquals(null, obj)) return false;
      if (ReferenceEquals(this, obj)) return true;
      return obj.x == x && obj.y == y;
    }

    public override int GetHashCode(){
      unchecked{
        return (x.GetHashCode()*397) ^ y.GetHashCode();
      }
    }
  }
}
